//
//  CouponListViewController.m
//  customer
//
//  Created by Tank on 2019/4/1.
//  Copyright © 2019 yinlong. All rights reserved.
//

#import "CouponListViewController.h"
#import "CouponTableViewCell.h"
#import "CouponListModel.h"
@interface CouponListViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation CouponListViewController

static NSString *CouponListTableCellID = @"CouponListTableCellID";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createCouponsView];
    self.title = @"优惠券";
}

- (void)createCouponsView{
    AutoWeakSelf
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(NAVIGATION_BAR_HEIGHT);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_offset(F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(0));
    }];
    self.refreshEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[CouponTableViewCell class] forCellReuseIdentifier:CouponListTableCellID];
}

- (void)loadDataList{
    AutoWeakSelf
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
    [dict setObject:[NSString emptyStr:K_UserDefault_Read(CustomerUserID)] forKey:@"member_id"];
    [dict setObject:[NSString emptyStr:K_UserDefault_Read(CustomerUserToken)] forKey:@"member_token"];
    [dict setObject:[NSString emptyStr:K_UserDefault_Read(CustomerUserAccount)] forKey:@"member_account"];
    if (self.page == 1) { [self.dataList removeAllObjects]; }
    [self showLoading];
    [[QYNetManager sharedManager]POST: kGetCouponList parameters:dict andApiTyp:CustomerApiTypGetOrders SuccessBlock:^(id responseObject) {
        [weakSelf hideLoading];
        [weakSelf endReresh];
        id resultData = responseObject[@"data"];
        if ([responseObject[@"status"] isEqualToString:@"error"]) {
            [weakSelf showErrorTipWithTitle:responseObject[@"error"]];
        }else if ([responseObject[@"status"] isEqualToString:@"ok"]){
            NSLog(@"%@", responseObject);
            NSArray *data = resultData[@"data"];
            if (![data isKindOfClass:[NSNull class]] && [data isKindOfClass:[NSArray class]] && [data count]){
                [weakSelf.tableView.mj_footer removeFromSuperview];
                for (NSDictionary *dict in data) {
                    CouponListModel *model = [CouponListModel yy_modelWithJSON:dict];
                    [self.dataList addObject:model];
                }
                [weakSelf.tableView reloadData];
                weakSelf.tableView.showEmpty = NO;
            }else if([resultData count] == 0){
                [weakSelf.dataList removeAllObjects];
                weakSelf.tableView.showEmpty = YES;
                [weakSelf showTipWithTitle:@"暂无优惠券"];
            }
        }else{
            [weakSelf showErrorTipWithTitle:@"获取优惠券失败"];
        }
        
    } Failure:^(NSError *error) {
        [weakSelf endReresh];
        [weakSelf hideLoading];
        weakSelf.tableView.showEmpty = YES;
        [weakSelf showErrorTipWithTitle:CustomerPoolNetStr];
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  [self.dataList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CouponTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CouponListTableCellID];
    [cell cellForLayoutCouponCell:self.dataList[indexPath.row]];
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 119;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
